package com.lumeng.spring.common.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author wanghui
 */
@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource(name="redisTemplate")
    private ValueOperations<String, String> valueOperations;
    @Resource(name="redisTemplate")
    private HashOperations<String, String, Object> hashOperations;
    @Resource(name="redisTemplate")
    private ListOperations<String, Object> listOperations;
    @Resource(name="redisTemplate")
    private SetOperations<String, Object> setOperations;
    @Resource(name="redisTemplate")
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;

    public void set(String key, Object value, long expire){
        valueOperations.set(key, toJson(value));
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value){
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public void delete(String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            delete(key);
        }
    }
    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }
    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, String hashKey, Object value) {
        hmSet(key, hashKey, value,NOT_EXPIRE);
    }
    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     * @param expire
     */
    public void hmSet(String key, String hashKey, Object value,long expire) {
        hashOperations.put(key, hashKey, value);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @param expire
     * @return
     */
    public Object hmGet(String key, String hashKey,long expire) {
        Object value = hashOperations.get(key, hashKey);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key, String hashKey) {
        return hmGet(key, hashKey, NOT_EXPIRE);
    }
    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @param clazz
     * @param expire
     * @return
     */
    public <T> T hmGet(String key, String hashKey, Class<T> clazz, long expire) {
        Object value = hashOperations.get(key,hashKey);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(toJson(value), clazz);
    }
    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @param clazz
     * @return
     */
    public <T> T hmGet(String key, String hashKey, Class<T> clazz) {
        return hmGet(key, hashKey, clazz, NOT_EXPIRE);
    }

    /**
     * 列表添加
     *
     * @param key
     * @param value
     * @param expire
     */
    public void lPush(String key, Object value, long expire) {
        listOperations.rightPush(key, value);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
    /**
     * 列表添加
     *
     * @param key
     * @param value
     */
    public void lPush(String key, Object value) {
        lPush(key, value, NOT_EXPIRE);
    }
    /**
     * 列表获取
     *
     * @param key
     * @param start
     * @param end
     * @param expire
     * @return
     */
    public List<Object> lRange(String key, long start, long end, long expire) {
        List<Object> value = listOperations.range(key, start, end);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
    /**
     * 列表获取
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public List<Object> lRange(String key, long start, long end) {
        return lRange(key, start, end, NOT_EXPIRE);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     * @param expire
     */
    public void sAdd(String key, Object value, long expire) {
        setOperations.add(key, value);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void sAdd(String key, Object value) {
        sAdd(key, value, NOT_EXPIRE);
    }
    /**
     * 集合获取
     *
     * @param key
     * @param expire
     * @return
     */
    public Set<Object> sMembers(String key, long expire) {
        Set<Object> value = setOperations.members(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> sMembers(String key) {
        return sMembers(key, NOT_EXPIRE);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     * @param expire
     */
    public void zAdd(String key, Object value, double scoure, long expire) {
        zSetOperations.add(key, value, scoure);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
    public void zAdd(String key, Object value, double scoure) {
        zAdd(key, value, scoure, NOT_EXPIRE);
    }
    /**
     * 有序集合获取
     *
     * @param key
     * @param min
     * @param max
     * @param expire
     * @return
     */
    public Set<Object> rangeByScore(String key, double min, double max, long expire) {
        Set<Object> value = zSetOperations.rangeByScore(key, min, max);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
    /**
     * 有序集合获取
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set<Object> rangeByScore(String key, double min, double max) {
        return rangeByScore(key, min, max, NOT_EXPIRE);
    }



    /**
     * Object转成JSON数据
     */
    private String toJson(Object object){
        if(object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String){
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz){
        return JSON.parseObject(json, clazz);
    }
}
